package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.model.Group;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.GroupGeofencesParam;
import com.lbssoft.gpsbd.model.vo.Move;
import com.lbssoft.gpsbd.service.DeviceService;
import com.lbssoft.gpsbd.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api("分组管理")
@RestController
@RequestMapping("/groups")
public class GroupController {


	@Autowired
	private GroupService groupService;

	@Autowired
	private DeviceService deviceService;

	@ApiOperation(value="获取分组数据", notes="获取分组数据")
	@RequiresPermissions("user:group:view")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list() {
		return groupService.list();
	}

	@ApiOperation(value="添加分组", notes="添加分组")
	@RequiresPermissions("user:group:add")
	@LogAnnotation(targetType = "group",action = "add",remark = "添加分组")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody Group group) {
		return  groupService.add(group);
	}


	@ApiOperation(value="删除分组", notes="删除分组")
	@RequiresPermissions("user:group:delete")
	@LogAnnotation(targetType = "group",action = "delete",remark = "删除分组")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@NotNull @PathVariable int id) {
		return groupService.delete(id);
	}

	@ApiOperation(value="修改分组", notes="修改分组")
	@RequiresPermissions("user:group:update")
	@LogAnnotation(targetType = "group",action = "update",remark = "修改分组")
	@ApiImplicitParam(paramType="body",name = "group", value = "分组信息", dataType = "Group")
	@RequestMapping(value = "",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@Validated @RequestBody Group group) {
		return groupService.update(group);
	}

	@ApiOperation(value="分组绑定围栏", notes="根据分组绑定围栏")
	@RequiresPermissions("user:geofence:bindgroup")
	@LogAnnotation(targetType = "groupgeofence",action = "update",remark = "分组绑定围栏")
	@RequestMapping(value = "bind",method = {RequestMethod.POST})
	@ResponseBody
	public Result groupBindGeofences(@RequestBody GroupGeofencesParam param) {
		return groupService.groupBindGeofences(param);
	}

	@ApiOperation(value="分组解除绑定围栏", notes="分组设备解除绑定的围栏")
	@RequiresPermissions("user:geofence:bindgroup")
	@LogAnnotation(targetType = "groupgeofence",action = "update",remark = "分组解除绑定围栏")
	@RequestMapping(value = "unbind",method = {RequestMethod.POST})
	@ResponseBody
	public Result groupUnbindGeofences(@RequestBody GroupGeofencesParam param) {
		return groupService.groupUnbindGeofences(param);
	}

	@ApiOperation(value="获取绑定的围栏", notes="根据分组id获取绑定的围栏ids")
	@RequestMapping(value = "/geofenceids/{id}",method = {RequestMethod.GET})
	@ResponseBody
	public Result getGeofenceIds(@PathVariable int id) {
		return groupService.getGeofenceIds(id);
	}

	@ApiOperation(value="移动分组设备", notes="移动分组设备")
	@LogAnnotation(targetType = "group",action = "update",remark = "移动分组设备")
	@ApiImplicitParam(paramType="body",name = "move", value = "分组信息", dataType = "Move")
	@RequiresPermissions("user:group:update")
	@RequestMapping(value = "/move",method = {RequestMethod.PUT})
	@ResponseBody
	public Result move(@Validated @RequestBody Move move) {
		Result result = new Result();
		if(move.getGroupId()==0){
			move.setGroupId(null);
		}
		if(move.getType().equals("group")){
			groupService.move(move);
		}else if(move.getType().equals("device")){
			deviceService.move(move);
		}

		return result;
	}
}